package ro.ubbcluj.cs.damate.plugin.ui.preferences;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import ro.ubbcluj.cs.damate.plugin.DamateActivator;
import ro.ubbcluj.cs.damate.plugin.settings.DamateSettings;

public class DamatePreferencesPage extends PreferencePage	implements IWorkbenchPreferencePage {
	
	Button captureEnabledCheckbox;

	IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {
		public void propertyChange(final PropertyChangeEvent event) {
			if (DamateSettings.CAPTURE_ENABLED_KEY.equals(event.getProperty())) {
				getControl().getDisplay().syncExec(new Runnable() {
					public void run() {
						captureEnabledCheckbox.setSelection((Boolean)event.getNewValue());
					};
				});				
			}
		}			
	};
	
	public DamatePreferencesPage() {
		setDescription("The Usage Data Collector collects information about how individuals are using the Eclipse platform. The intent is to use this data to help committers and organizations better understand how developers are using Eclipse."); 
		setPreferenceStore(DamateActivator.getDefault().getPreferenceStore());
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
		getPreferenceStore().addPropertyChangeListener(propertyChangeListener);
	}
	
	@Override
	public void dispose() {
		getPreferenceStore().removePropertyChangeListener(propertyChangeListener);
		super.dispose();
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		composite.setLayout(new GridLayout());
		
		createGeneralInformationArea(composite);
		
		Label filler = new Label(parent, SWT.NONE);
		filler.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
		
		initialize();
		
		return composite;
	}

	private void initialize() {
		captureEnabledCheckbox.setSelection(getCapturePreferences().getBoolean(DamateSettings.CAPTURE_ENABLED_KEY));
	}

	@Override
	public boolean performOk() {
		getCapturePreferences().setValue(DamateSettings.CAPTURE_ENABLED_KEY, captureEnabledCheckbox.getSelection());
	
		return super.performOk();
	}

	@Override
	protected void performDefaults() {
		captureEnabledCheckbox.setSelection(getCapturePreferences().getDefaultBoolean(DamateSettings.CAPTURE_ENABLED_KEY));

		super.performDefaults();
	}

	private void createGeneralInformationArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false));
		
		composite.setLayout(new GridLayout());
		
		captureEnabledCheckbox = new Button(composite, SWT.CHECK | SWT.LEFT);
		captureEnabledCheckbox.setText("Enable capture");  
	}


	private IPreferenceStore getCapturePreferences() {
		return DamateActivator.getDefault().getPreferenceStore();
	}
}
